package org.example.Example;

import org.example.Tool.Tool;

import java.util.Arrays;
import java.util.PriorityQueue;

public class Heap extends Tool {
    /**
     * 堆排序扩展
     * */
    public void sort(int[] arr, int target){
        int i=0;
        int []temp = arr;
        while (temp.length>0){
            heapify(temp,0, Math.min(target,temp.length-1));
            arr[i] = temp[0];
            temp = Arrays.copyOfRange(temp,1,arr.length-i); //模拟pop操作
            i++;
        }
    }
    public void heapify(int arr[],int index,int heapsize){
        int left = index*2+1;
        while(left<heapsize){
            int largest = left<heapsize&&arr[left+1]<arr[left]?left+1:left;
            largest = arr[largest]<arr[index]?largest:index;
            if(largest==index){
                break;
            }
            swap(arr,largest,index);
            left++;
        }
    }

    public static void main(String[] args) {
        int arr[] = {3,2,1,0,8,6,10,25,36,21,50};
        new Heap().sort(arr,6);
        Arrays.stream(arr).forEach(System.out::println);
    }
}
